package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.EnrollrefreshMapper;
import com.xgame.server.db.model.Enrollrefresh;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class EnrollrefreshCsTool {
    @Resource
    private EnrollrefreshMapper enrollrefreshMapper;

    //插入经验副本
    public Integer insertEnrollRefresh(Game.EnrollRefreshCS data) throws Exception {
        //1.删除本玩家的所有
        deleteEnrollRefresh(data);

        //2.插入
        Enrollrefresh map = new Enrollrefresh();
        map.setAccount(data.getAccount());
        map.setPlayerId(data.getPlayerID());
        map.setIdentity(data.getIdentify());

        String lst = "";
        List<Game.EnrollRefreshOneCS> items = data.getListList();
        int length = items.size();
        Integer count = new Integer(0);
        for (int i = 0; i < length; i++) {
            Game.EnrollRefreshOneCS one = items.get(i);

            if (i == length - 1) {
                //lst = lst + items.get(i).getType() + ";" + items.get(i).getLastDieTime() + ";" + items.get(i).getSpawnInterval();
            } else {
                //lst = lst + items.get(i).getType() + ";" + items.get(i).getLastDieTime() + ";" + items.get(i).getSpawnInterval() + "^";
            }
        }

        map.setData(lst);
        count += enrollrefreshMapper.insert(map);

        return count;
    }

    //删除记录
    public Integer deleteEnrollRefresh(Game.EnrollRefreshCS data) throws Exception {
        EntityWrapper<Enrollrefresh> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        return enrollrefreshMapper.delete(userWrapper);
    }

    //查询记录
    public List<Enrollrefresh> selectEnrollRefresh(String account) throws Exception {
        EntityWrapper<Enrollrefresh> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return enrollrefreshMapper.selectList(userWrapper);
    }
}
